package com.stockz.portfolio.domain.model;

import java.util.Date;

import com.stockz.core.model.Scrip;
import com.stockz.core.util.SpringUtil;

public class Transaction  implements Comparable<Transaction>{

	private TransactionType type;
	private int quantity;
	private double price;
	private Scrip scrip;
	private Date date;
	private Broker broker;
	
	public Transaction() {
		
	}
	
	public Transaction(TransactionType type, int quantity, double price,
			Scrip scrip, Date date, Broker broker) {
		super();
		this.type = type;
		this.quantity = quantity;
		this.price = price;
		this.scrip = scrip;
		this.date = date;
		this.broker = broker;
		validate();
	}
	
	public void validate(){
		if(type == null){
			throw new IllegalArgumentException(
					SpringUtil.getApplicationContext().getMessage(
							"transaction.type.can.not.be.null", null, null));
		}
		if(quantity <= 0){
			throw new IllegalArgumentException(
					SpringUtil.getApplicationContext().getMessage(
							"transaction.quantity.must.be.more.than.zero", null, null));
		}
		if(price <= 0){
			throw new IllegalArgumentException(
					SpringUtil.getApplicationContext().getMessage(
							"transaction.price.must.be.more.than.zero", null, null));
		}
		if(scrip == null){
			throw new IllegalArgumentException(
					SpringUtil.getApplicationContext().getMessage(
							"transaction.scrip.can.not.be.null", null, null));
		}
		if(date == null){
			throw new IllegalArgumentException(
					SpringUtil.getApplicationContext().getMessage(
							"transaction.date.can.not.be.null", null, null));
		}
	}

	@Override
	public int compareTo(Transaction o) {
		if(o != null && o.getDate() != null && getDate() != null){
			return getDate().compareTo(o.getDate());
		}
		return 0;
	}
	
	public double getAmount(){
		if(broker == null){
			return price * quantity;
		}else{
			return (price * quantity)*(1+broker.getBrokarage()/100);
		}
	}
	
	public TransactionType getType() {
		return type;
	}
	public void setType(TransactionType type) {
		this.type = type;
	}
	public int getQuantity() {
		return quantity;
	}
	public void setQuantity(int quantity) {
		this.quantity = quantity;
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	public Scrip getScrip() {
		return scrip;
	}
	public void setScrip(Scrip scrip) {
		this.scrip = scrip;
	}
	public Date getDate() {
		return date;
	}
	public void setDate(Date date) {
		this.date = date;
	}
	public Broker getBroker() {
		return broker;
	}
	public void setBroker(Broker broker) {
		this.broker = broker;
	}
	
	
}
